<template>
    <el-card>
        <el-alert type="warning" title="快速集成开放文档：http://fizzgate.com/guide/manager/manager_open_document_use.html"></el-alert>    
        <el-form :model="queryParams" ref="queryParams" :inline="true" label-width="120px">
            <el-form-item label="环境：" prop="env">
                <el-input v-model="queryParams.env" placeholder="名称" size="small"></el-input>
            </el-form-item>
             <el-form-item >
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-delete" size="small" @click="resetQuery('queryParams')">清空</el-button>
            </el-form-item>
        </el-form>
         <el-row :gutter="10">
            <el-col :span="24">
                <div style="margin-bottom: 10px;padding: 0 10px">
                    <el-button type="primary" icon="el-icon-plus" size="small" @click="handleAdd">新增</el-button>
                </div>
            </el-col>
        </el-row>
        <el-table :data="tableData" size="mini" border style="width: 100%">
             <el-table-column type="index" label="#" width="55"></el-table-column>
             <el-table-column prop="env" label="环境"></el-table-column>
             <el-table-column prop="reqUrl" label="请求地址"></el-table-column>
             <el-table-column label="操作" width="200">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelte(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                v-show="total>0"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                background
                :current-page="1"
                :page-sizes="[10, 20, 30, 40,50,100]"
                :page-size="10"
                layout="total, sizes, prev, pager, next, jumper"
                class="pagination-container"
                :total="total">
        </el-pagination>
        <el-dialog :title="formTitle" width="30%" :visible.sync="dialogVisible">
            <el-form :model="addForm" ref="addForm" :rules="rules" label-width="100px" :disabled="isView">
              <el-form-item label="环境：" prop="env" required>
                  <el-input v-model="addForm.env" placeholder="环境" size="small"></el-input>
              </el-form-item>
              <el-form-item label="请求地址：" prop="reqUrl" required>
                  <el-input v-model="addForm.reqUrl" placeholder="请求地址" type="textarea" rows="3" size="small"></el-input>
              </el-form-item>
            </el-form>
             <div slot="footer" class="dialog-footer" v-if="!isView">
                <el-button type="primary" @click="handleSubmit('addForm')" size="small">保存</el-button>
                <el-button @click="addFormVisible = false" size="small">取消</el-button>
            </div>
        </el-dialog>
    </el-card>
</template>

<script>
    import { getAddressList,save,update,remove } from '@/api/interface/address.api'
    export default {
        data() {
            return {
                queryParams: {
                    current: 1,
                    size: 10,
                    env: undefined
                },
                total: 0,
                tableData: [],
                formTitle: '新增环境信息',
                addForm: {},
                rules: {
                    env: [{ required: true, message: '请输入环境', trigger: 'blur' }],
                    reqUrl: [
                        { required: true, message: '请输入请求地址', trigger: 'blur' },
                        { pattern: /(https?|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/,message: 'URL格式不正确！',trigger: 'blur'}
                    ],
                },
                dialogVisible: false,
                isView: false
            }
        },
        mounted() {
            this.getList();
        },
        methods: {
             async getList() {
                const result = await getAddressList(this.queryParams);
                if(result.code === 200) {
                    this.tableData = result.data.records;
                    this.total = result.data.total
                }
            },
              // 搜索
            handleQuery() {
                this.queryParams.current = 1;
                this.getList();
            },
            // 清空
            resetQuery(formName) {
                this.$refs[formName].resetFields();
                this.getList();
            },
            handleSizeChange(val) {
                this.queryParams.size = val;
                this.getList();
            },
            handleCurrentChange(val) {
                this.queryParams.current = val;
                this.getList();
            },
            handleAdd() {
                this.formTitle = '新增环境信息';
                this.dialogVisible = true;
            },
            handleEdit(row) {
                this.formTitle = '编辑环境信息';
                this.addForm = JSON.parse(JSON.stringify(row));
                this.isView = false;
                this.dialogVisible = true;
            },
            handleView(row) {
                this.formTitle = '查看环境信息';
                this.addForm = row;
                this.isView = true;
                this.dialogVisible = true;
            },
            handleSubmit(formName) {
                this.$refs[formName].validate(async (valid) => {
                    if (valid) {
                        if(this.addForm.id) {
                            const result = await update(this.addForm);
                            if(result.code === 200) {
                                this.$message.success('修改成功');
                            }
                        }else {
                             const result = await save(this.addForm);
                             if(result.code === 200) {
                                this.$message.success('保存成功');
                             }
                        }
                         this.dialogVisible = false;
                         this.getList();
                       
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
             handleDelte(id) {
                this.$confirm('确定将选择的数据删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                    }).then(async () => {
                        const result = await remove(id);
                        if(result.code === 200) {
                             this.$message({
                                type: 'success',
                                message: '删除成功!'
                           });
                           this.getList();
                        }
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });          
                });
            },
        }
    }
</script>

<style lang="scss" scoped>
.pagination-container {
    padding: 10px;
    text-align: right;
}
</style>